/*******************************************************************************
 * Copyright（C） CETC-32
 * @file:MyContextListener.java
 * @Description：
 * @Author ：徐文远
 * @version：1.0
 * @date ： 2021/5/7 下午4:44
 ******************************************************************************/

package com.cetc32.webutil.client.listener;

import com.cetc32.webutil.client.bean.SSOProperties;
import com.cetc32.webutil.client.bean.URLPermissions;
import com.cetc32.webutil.common.util.HttpclientUtil;
import com.cetc32.webutil.exception.S32Exception;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
 * 监听器，容器启动后监听事件触发，负责校验数据
 * **/
@Component
@Order
public class MyContextListener implements ApplicationRunner {
    @Autowired
    URLPermissions urlPermissions;

    @Override
    public void run(ApplicationArguments applicationArguments) throws Exception {
        //check();
        uploadSysInfo();
    }
    /***
     * verifyUrl、passportUrl已经由 Spring容器托管
     * getDefaultLoginRequiredEnable 为非强制关联，
     * 当系统采用注解模式时候，二者不需要关联校验
     * **/
    @Deprecated
    private void check(){
        SSOProperties sso = urlPermissions.getSsoProperties();
        if(StringUtils.isBlank(sso.getVerifyUrl()))
            throw new S32Exception("Invalid configuration is found ,please make sure ${sso.verifyUrl} is configured.");
        if(StringUtils.isBlank(sso.getPassportUrl()))
            throw new S32Exception("Invalid configuration is found ,please make sure ${sso.passportUrl} is configured.");
        if(sso.getDefaultLoginRequiredEnable() == true && sso.getLoginExclude() == null
                || sso.getDefaultLoginRequiredEnable() == false && sso.getLoginInclude() ==null )
            throw new S32Exception("Invalid configuration is found ,please make sure ${sso.DefaultLoginRequiredEnable},${sso.loginInclude} and ${sso.loginExclude} is rightly configured .");
    }

    /****
     * 每次服务启动完成之后，加载上传当前的系统的权限名等信息
     * 认证中心负责匹配更新校验
     * ***/
    private void uploadSysInfo() {
        Set<String> codes = urlPermissions.getCode();
        SSOProperties ssoProperties =urlPermissions.getSsoProperties();
        String paramC = codes.toString()
                .replaceAll("\\[","")
                .replaceAll("]","")
                .replaceAll(" ","")
                .replaceAll(",","");
        if(StringUtils.isBlank(ssoProperties.getPermission())
                || StringUtils.isBlank(ssoProperties.getRegisterUrl()))
            return;
        Map<String,String> params =new HashMap<>();
        params.put("permission",paramC);
        params.put("sysname",ssoProperties.getSysname());
        params.put("sysurl",ssoProperties.getSysurl());
        HttpclientUtil.doPost(ssoProperties.getRegisterUrl(),params);

    }
}
